package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.Comment;
import com.production.CoursePlatform.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
public class CommentController {
    //此接口已检验
    @Autowired
    private CommentService commentService;

    // 创建新的评论
    @PostMapping("/create")
    public ResponseEntity<String> createComment(@RequestBody Comment comment) {
        commentService.createComment(comment);
        return ResponseEntity.ok("Comment created successfully!");
    }

    // 根据dID查找评论
    @GetMapping("get/{dID}")
    public ResponseEntity<List<Comment>> getCommentByCmtId(@PathVariable int dID) {
        List<Comment> comments = commentService.findCommentByCmtId(dID);
        if (comments != null) {
            return ResponseEntity.ok(comments);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    // 删除指定cmtID的评论
    @DeleteMapping("delete/{cmtID}")
    public ResponseEntity<String> deleteComment(@PathVariable int cmtID) {
        boolean isDeleted = commentService.deleteComment(cmtID);
        if (isDeleted) {
            return ResponseEntity.ok("Comment deleted successfully!");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Comment not found");
        }
    }

    // 更新评论
    @PutMapping("/update")
    public ResponseEntity<String> updateComment(@RequestBody Comment comment) {
        boolean isUpdated = commentService.updateComment(comment);
        if (isUpdated) {
            return ResponseEntity.ok("Comment updated successfully!");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Comment not found");
        }
    }
}
